package com.jielin.handling.third.xsky;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.HttpMethod;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
import sun.rmi.runtime.Log;

import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class XskyS3Object {

    public AmazonS3 client = null;

    public XskyS3Object(AmazonS3 client) {
        this.client = client;
    }

    // 获取对象列表
    public List<S3ObjectSummary> ListingObjects(String bucketName) {
        ListObjectsV2Result objects = null;
        try {
            objects = client.listObjectsV2(bucketName);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when list objects of \"" + bucketName + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
        return objects.getObjectSummaries();
    }

    // 使用分页操作列出大量对象
    public void listObjects(String bucketName) {
        ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucketName);
        ListObjectsV2Result result;
        try {
            do {
                result = client.listObjectsV2(req);
                for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
                    System.out.printf(" - %s (size: %d)\n", objectSummary.getKey(), objectSummary.getSize());
                }
                //获取继续获取对象列表的Token
                String token = result.getNextContinuationToken();
                System.out.println("Next Continuation Token: " + token);
                req.setContinuationToken(token);
            } while (result.isTruncated());
        } catch (AmazonServiceException e) {
            e.printStackTrace();
        }
    }

    // 整体上传
    public PutObjectResult PutObject(String bucketName, String key, File file) {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.addUserMetadata("Use_test", "test");
        PutObjectRequest putRequest = new PutObjectRequest(bucketName, key, file);
        putRequest.setMetadata(metadata);
        PutObjectResult putResult = null;
        try {
            putResult = client.putObject(putRequest);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when put object of \"" + key + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
        return putResult;
    }

    // 通过文件流的方式上传对象
    public PutObjectResult PutObjectRequest(String bucketName, String key, File file) {
        InputStream putStream = null;
        PutObjectResult putResult = null;
        try {
            putStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.addUserMetadata("Use", "test");
        PutObjectRequest putRequest = new PutObjectRequest(bucketName, key, putStream, metadata);
        try {
            putResult = client.putObject(putRequest);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when put object of \"" + key + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
        return putResult;
    }

    // 分段上传
    public String multiPutObject(String bucketName, String key, File file, List<PartETag> partETags) {
        InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initResult = null;

        //创建分段上传
        try {
            initResult = client.initiateMultipartUpload(initRequest);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when create MultipartUpload of \"" + key + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }

        long fileLength = file.length();
        long partSize = 5 * 1024 * 1024;//5M,分段上传最小分段为5M
        long filePosition = 0;
        UploadPartRequest upRequest = new UploadPartRequest();

        upRequest.setBucketName(bucketName);
        upRequest.setKey(key);
        upRequest.setFile(file);
        upRequest.setUploadId(initResult.getUploadId());

        //上传文件
        for (int i = 1; filePosition < fileLength; i++) {
            partSize = Math.min(partSize, (fileLength - filePosition));
            upRequest.setPartNumber(i);
            upRequest.setFileOffset(filePosition);
            upRequest.setPartSize(partSize);
            try {
                UploadPartResult uploadResult = client.uploadPart(upRequest);
                partETags.add(uploadResult.getPartETag());
            } catch (AmazonServiceException ase) {
                System.out.println("Caught an AmazonServiceException when upload part of \"" + key + "\".");
                System.out.println("Error Message:    " + ase.getMessage());
                System.out.println("HTTP Status Code: " + ase.getStatusCode());
                System.out.println("AWS Error Code:   " + ase.getErrorCode());
                System.out.println("Error Type:       " + ase.getErrorType());
                System.out.println("Request ID:       " + ase.getRequestId());
                System.exit(1);
            }
            filePosition += partSize;
        }
        return initResult.getUploadId();
    }

    //完成分段上传
    public void finish(String bucketName, String key, String uploadId, List<PartETag> partETags) {
        CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        try {
            client.completeMultipartUpload(compRequest);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when finish MultipartUpload of \"" + key + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
    }

    // 终止分段上传
    public void abortMultiUload(String bucketName, String key, String uploadId) {
        AbortMultipartUploadRequest abortUpload = new AbortMultipartUploadRequest(bucketName, key, uploadId);
        try {
            client.abortMultipartUpload(abortUpload);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when abort upload of \"" + key + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
    }

    // 列出指定分段上传的part
    public PartListing listParts(String bucketName, String key, String uploadId) {
        ListPartsRequest partRequest = new ListPartsRequest(bucketName, key, uploadId);
        try {
            PartListing partLists = client.listParts(partRequest);
            return partLists;
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when list MultipartUpload of \"" + key + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
        return null;
    }

    // 获取对象
    public S3Object getObject(String bucketName, String key) {
        try {
            return client.getObject(new GetObjectRequest(bucketName, key));
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when get object of \"" + key + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
        return null;
    }

    // 获取对象
    public S3Object getObjectThrows(String bucketName, String key) throws SdkClientException {
//        try {
        return client.getObject(new GetObjectRequest(bucketName, key));
//        } catch (AmazonServiceException ase) {
//            System.out.println("Caught an AmazonServiceException when get object of \"" + key + "\".");
//            System.out.println("Error Message:    " + ase.getMessage());
//            System.out.println("HTTP Status Code: " + ase.getStatusCode());
//            System.out.println("AWS Error Code:   " + ase.getErrorCode());
//            System.out.println("Error Type:       " + ase.getErrorType());
//            System.out.println("Request ID:       " + ase.getRequestId());
//            System.exit(1);
//        }
//        return null;
    }

    // 获取对象元数据
    public ObjectMetadata getMetaData(String bucketName, String key) {
        try {
            return client.getObjectMetadata(bucketName, key);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when get metadata of \"" + key + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
        return null;
    }

    // 单个删除对象
    public void DeletingAnObject(String bucketName, String key) {
        try {
            client.deleteObject(bucketName, key);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when delete object of \"" + key + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
    }

    // 删除带Version的对象
    public void deleteObjetcWithVersion(String bucketName, String key, String versionId) {
        try {
            client.deleteVersion(bucketName, key, versionId);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when delete object with version of \"" + key + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
    }

    // 批量删除
    public void deleteObjectS(String bucketName, List<DeleteObjectsRequest.KeyVersion> listKeys) {
        DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucketName);
        deleteRequest.withKeys(listKeys);
        try {
            client.deleteObjects(deleteRequest);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when delete objects as below:");
            for (DeleteObjectsRequest.KeyVersion key : listKeys) {
                System.out.println("  key: " + key.getKey() + "; Version: " + key.getVersion());
            }
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
    }

    // 获取对象version id
    public void getObjectVersion(String bucketName, String prefix) {
        ListVersionsRequest listRequest = new ListVersionsRequest()
                .withBucketName(bucketName)
                .withPrefix(prefix);
        VersionListing versionList;
        try {
            versionList = client.listVersions(listRequest);
            for (S3VersionSummary version : versionList.getVersionSummaries()) {
                System.out.println(version.getKey() + ":" + version.getVersionId());
            }
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when list version of \"" + bucketName + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
    }

    //获取对象预签名URL
    public void getURL(String bucketName, String key, long expTimeMillis, HttpMethod method) {

        Date expiration = new java.util.Date();
        expiration.setTime(expTimeMillis);

        // 生成URL
        GeneratePresignedUrlRequest generatePresignedUrlRequest =
                new GeneratePresignedUrlRequest(bucketName, key)
                        .withMethod(method)
                        .withExpiration(expiration);

        URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
        System.out.println("Pre-Signed URL: " + url.toString());
    }

    // 复制对象
    public void copyObeject(String sourceBucketName,
                            String sourceKey,
                            String destinationBucketName,
                            String destinationKey) {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.addUserMetadata("Source", "copy");
        CopyObjectRequest copyReq = new CopyObjectRequest(sourceBucketName, sourceKey, destinationBucketName, destinationKey)
                .withNewObjectMetadata(metadata);
        try {
            client.copyObject(copyReq);
            System.out.println("Success to copy the object: " + sourceKey);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when copy object of \"" + sourceBucketName + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
    }

    public void copyObejectThrows(String sourceBucketName, String sourceKey, String destinationBucketName, String destinationKey, ObjectMetadata metadata) throws SdkClientException {
        metadata.addUserMetadata("Source", "copy");
        // 设置所有用户可读权限
        AccessControlList acl = new AccessControlList();
        acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);
        CopyObjectRequest copyReq = new CopyObjectRequest(sourceBucketName, sourceKey, destinationBucketName, destinationKey)
                .withNewObjectMetadata(metadata).withAccessControlList(acl);

//        try {
        client.copyObject(copyReq);
        System.out.println("Success to copy the object: " + sourceKey);
//        } catch (AmazonServiceException ase) {
//            System.out.println("Caught an AmazonServiceException when copy object of \"" + sourceBucketName + "\".");
//            System.out.println("Error Message:    " + ase.getMessage());
//            System.out.println("HTTP Status Code: " + ase.getStatusCode());
//            System.out.println("AWS Error Code:   " + ase.getErrorCode());
//            System.out.println("Error Type:       " + ase.getErrorType());
//            System.out.println("Request ID:       " + ase.getRequestId());
//            System.exit(1);
//        }
    }

    // 采用分段上传API进行大对象复制
    public void copyObejectUseMulti(String sourceBucketName,
                                    String sourceKey,
                                    String destinationBucketName,
                                    String destinationKey) {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.addUserMetadata("Source", "copy");
        InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(destinationBucketName, destinationKey);
        initRequest.setObjectMetadata(metadata);
        InitiateMultipartUploadResult initResult = client.initiateMultipartUpload(initRequest);

        try {
            GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest(sourceBucketName, sourceKey);
            ObjectMetadata metadataResult = client.getObjectMetadata(metadataRequest);
            long objectSize = metadataResult.getContentLength();

            long partSize = 5 * 1024 * 1024;
            long bytePosition = 0;
            int partNum = 1;
            List<CopyPartResult> copyResponses = new ArrayList<CopyPartResult>();
            while (bytePosition < objectSize) {
                //最后一个part可能比5M小
                long lastByte = Math.min(bytePosition + partSize - 1, objectSize - 1);
                CopyPartRequest copyRequest = new CopyPartRequest()
                        .withSourceBucketName(sourceBucketName)
                        .withSourceKey(sourceKey)
                        .withDestinationBucketName(destinationBucketName)
                        .withDestinationKey(destinationKey)
                        .withUploadId(initResult.getUploadId())
                        .withFirstByte(bytePosition)
                        .withLastByte(lastByte)
                        .withPartNumber(partNum++);
                copyResponses.add(client.copyPart(copyRequest));
                bytePosition += partSize;
            }

            CompleteMultipartUploadRequest completeRequest = new
                    CompleteMultipartUploadRequest(destinationBucketName, destinationKey, initResult.getUploadId(), getETags(copyResponses));
            client.completeMultipartUpload(completeRequest);

            System.out.println("Multipart copy complete of object:" + sourceKey);
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException when copy object of \"" + sourceBucketName + "\".");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            System.exit(1);
        }
    }

    // This is a helper function to construct a list of ETags.
    private static List<PartETag> getETags(List<CopyPartResult> responses) {
        List<PartETag> etags = new ArrayList<PartETag>();
        for (CopyPartResult response : responses) {
            etags.add(new PartETag(response.getPartNumber(), response.getETag()));
        }
        return etags;
    }

    public void listversion() {
        String bucketName = "liudonghai";
        VersionListing versionList = client.listVersions(new ListVersionsRequest()
                .withBucketName(bucketName)
                .withMaxResults(1)
                .withDelimiter("\\"));
        while (true) {
            Iterator<S3VersionSummary> versionIter = versionList.getVersionSummaries().iterator();
            while (versionIter.hasNext()) {
                S3VersionSummary vs = versionIter.next();
                //client.deleteVersion(bucketName, vs.getKey(), vs.getVersionId());
                System.out.println(vs.getVersionId());
            }

            if (versionList.isTruncated()) {
                versionList = client.listNextBatchOfVersions(versionList);
            } else {
                break;
            }
        }
    }

    // 通过file上传并抛出异常
    public PutObjectResult PutObjectThrows(String bucketName, String key, File file) throws SdkClientException {
        PutObjectRequest putRequest = new PutObjectRequest(bucketName, key, file);
        if (file != null && file.exists()) {
            ObjectMetadata metadata = new ObjectMetadata();
//            String contentType = new MimetypesFileTypeMap().getContentType(file);
            String contentType = null;
//            if (ContentType.APPLICATION_OCTET_STREAM.getMimeType().equals(contentType)) {
            Path path = Paths.get(file.getPath());
            try {
                contentType = Files.probeContentType(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
//            }
//            System.out.println(contentType + "==========" + path);
            metadata.setContentType(contentType);
            putRequest.setMetadata(metadata);
        }
        // 设置所有用户可读权限
        AccessControlList acl = new AccessControlList();
        acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);
        putRequest.withAccessControlList(acl);
        putRequest.getRequestClientOptions().setReadLimit((int) file.length());

        PutObjectResult putResult = null;
//        try {
        putResult = client.putObject(putRequest);
//        } catch (AmazonServiceException ase) {
//            System.out.println("Caught an AmazonServiceException when put object of \"" + key + "\".");
//            System.out.println("Error Message:    " + ase.getMessage());
//            System.out.println("HTTP Status Code: " + ase.getStatusCode());
//            System.out.println("AWS Error Code:   " + ase.getErrorCode());
//            System.out.println("Error Type:       " + ase.getErrorType());
//            System.out.println("Request ID:       " + ase.getRequestId());
//            System.exit(1);
//        }
        return putResult;
    }

    // 通过流上传并抛出异常
    public PutObjectResult PutObject(String bucketName, String key, byte[] bytes, ObjectMetadata metadata) throws SdkClientException {
        /*if (metadata == null) {
            metadata.addUserMetadata("Use_test", "test");
        }*/

        InputStream is = new ByteArrayInputStream(bytes);
        PutObjectRequest putRequest = new PutObjectRequest(bucketName, key, is, metadata);
        // 设置所有用户可读权限
        AccessControlList acl = new AccessControlList();
        acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);
        putRequest.withAccessControlList(acl);
        putRequest.getRequestClientOptions().setReadLimit(bytes.length);

        PutObjectResult putResult = null;
//        try {
        putResult = client.putObject(putRequest);
        URL url = client.getUrl(bucketName, key);
//        } catch (AmazonServiceException ase) {
//            System.out.println("Caught an AmazonServiceException when put object of \"" + key + "\".");
//            System.out.println("Error Message:    " + ase.getMessage());
//            System.out.println("HTTP Status Code: " + ase.getStatusCode());
//            System.out.println("AWS Error Code:   " + ase.getErrorCode());
//            System.out.println("Error Type:       " + ase.getErrorType());
//            System.out.println("Request ID:       " + ase.getRequestId());
//            System.exit(1);
//        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return putResult;
    }

    // 通过流上传并抛出异常
    public PutObjectResult PutObject(String bucketName, String key, InputStream is, ObjectMetadata metadata) throws SdkClientException {
        /*if (metadata == null) {
            metadata.addUserMetadata("Use_test", "test");
        }*/
        int size = 0;
        try {
            size = is.available();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("===============================size:" + size);

        PutObjectRequest putRequest = new PutObjectRequest(bucketName, key, is, metadata);
        // 设置所有用户可读权限
        AccessControlList acl = new AccessControlList();
        acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);
        putRequest.withAccessControlList(acl);
        // 设置大小接近4G
//        putRequest.getRequestClientOptions().setReadLimit(1000000 * 1024 * 4);
        // 设置大小接近500MB.
//        putRequest.getRequestClientOptions().setReadLimit(500000000);
        putRequest.getRequestClientOptions().setReadLimit(size * 1024);

        PutObjectResult putResult = null;
//        try {
        putResult = client.putObject(putRequest);
        URL url = client.getUrl(bucketName, key);
//        } catch (AmazonServiceException ase) {
//            System.out.println("Caught an AmazonServiceException when put object of \"" + key + "\".");
//            System.out.println("Error Message:    " + ase.getMessage());
//            System.out.println("HTTP Status Code: " + ase.getStatusCode());
//            System.out.println("AWS Error Code:   " + ase.getErrorCode());
//            System.out.println("Error Type:       " + ase.getErrorType());
//            System.out.println("Request ID:       " + ase.getRequestId());
//            System.exit(1);
//        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return putResult;
    }

    // 直接上传字符串对象
    public PutObjectResult PutObject(String bucketName, String key, String content) throws SdkClientException {
        PutObjectResult putResult = null;
//        try {
        putResult = client.putObject(bucketName, key, content);
//        } catch (AmazonServiceException ase) {
//            System.out.println("Caught an AmazonServiceException when put object of \"" + key + "\".");
//            System.out.println("Error Message:    " + ase.getMessage());
//            System.out.println("HTTP Status Code: " + ase.getStatusCode());
//            System.out.println("AWS Error Code:   " + ase.getErrorCode());
//            System.out.println("Error Type:       " + ase.getErrorType());
//            System.out.println("Request ID:       " + ase.getRequestId());
//            System.exit(1);
//        }
        return putResult;
    }

}
